package test3;


import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author liule
 * @date 2023/8/17 15:45
 * @Desc : 练习Thread，实现多线程同步下载图片
 */
public class TestThread3_2 extends Thread {
    private String url;
    private String fileName;
    public TestThread3_2(String url,String fileName){
        this.url = url;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        WebDownLoader.downloader(url,fileName);
        System.out.println("succes download:" +fileName);
    }

    public static void main(String[] args) {
        TestThread3_2 t1 = new TestThread3_2("https://img0.baidu.com/it/u=3021883569,1259262591&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1692378000&t=c466fc9fc82d323f075a38cbb7c6c77c","1.png");
        TestThread3_2 t2 = new TestThread3_2("https://img1.baidu.com/it/u=3663508195,2907650417&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1692378000&t=9f11a3e494efccd0dad4d4ce8fd1d627","2.png");
        TestThread3_2 t3 = new TestThread3_2("https://img0.baidu.com/it/u=3870526223,1678892290&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1692378000&t=810e3a8e26d36ee9e63a6ee9118a0398","3.png");
        t1.start();
        t2.start();
        t3.start();
    }
}
//下载器
class WebDownLoader{
    // 下载方法
    public static void downloader(String url, String fileName){
        //commons-io工具类
        try {
            FileUtils.copyURLToFile(new URL(url),new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，WebDownLoader downloader方法出现问题");
        }
    }
}